/*
    Copyright Nicolas Ronvel 2007
	gulix33xp@yahoo.fr

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

using System;
using System.Diagnostics;
using System.Drawing;

using SdlDotNet.Graphics;
using Sdl = SdlDotNet.Graphics;
using SdlDotNet.Core;
using SdlDotNet.Input;

namespace BlindShark.GameStates
{
	/// <summary>
	/// Display the Title Screen of Blind Shark
	/// </summary>
	public class GS_TitleScreen : GameState
	{
		#region Variables
		
		Surface _sfLogo;
		// Fonts
		string _sFont;
		Sdl.Font _fTitle;
		Sdl.Font _fSubtitle;
		Sdl.Font _fWeb;
		Sdl.Font _fPressSpaceKey;
		Sdl.Font _fVersion;
		// Displayed strings
		string _sTitle;
		string _sSubtitle;
		string _sWeb;
		string _sPress;
		string _sVersion;
		
		bool _bDisplayPress;
		
		// Elements location
		Rectangle _rctLogo;
		Rectangle _rctTitle;
		Rectangle _rctSubtitle;
		Rectangle _rctWeb;
		Rectangle _rctPress;
		Rectangle _rctVersion;
		
		#endregion
		
		#region Constructors
		public GS_TitleScreen() : base()
		{
			_sFont = "fonts/Fertigo_17b.otf";
			
			InitTexts();
			
			// 3 seconds have to be waited before "Press Escape" is displayed
			_bDisplayPress = false;
			ClockManager.AddTimeEvent(new TimeEvent(1,3000, new TimeEvent.Call(ActivePressDisplay)));
		}
		#endregion
		
		#region GameState interface members
		public void CleanUp()
		{
			_sfLogo = null;
		}
		
		public void HandleEvents()
		{
			while (Events.Poll())
			{
				KeyboardState state = new KeyboardState();
				// Escape quits the program
				if (state[Key.Escape])
					GameStateManager.Quit();
				// Space starts the program
				else if (state[Key.Space])
					GameStateManager.ChangeState(new GS_MenuMain());
			}
		}
		
		public void Update()
		{
			// No Updating actions
		}
		
		public void Draw()
		{
			ScreenManager.MainScreen.Fill(Color.Black);
			// Displaying the logo
			ScreenManager.MainScreen.Blit(_sfLogo, _rctLogo.Location);
			// Displaying the title
			ScreenManager.MainScreen.Blit(_fTitle.Render(_sTitle, Color.AntiqueWhite), _rctTitle.Location);
			// Displaying the subtitle
			ScreenManager.MainScreen.Blit(_fSubtitle.Render(_sSubtitle, Color.AntiqueWhite), _rctSubtitle.Location);
			// Displaying the web information
			ScreenManager.MainScreen.Blit(_fWeb.Render(_sWeb, Color.LightBlue), _rctWeb.Location);
			// Displaying the version information
			ScreenManager.MainScreen.Blit(_fVersion.Render(_sVersion, Color.LightGray), _rctVersion.Location);
			// Displaying the "Press Escape" message
			if (_bDisplayPress)
				ScreenManager.MainScreen.Blit(_fPressSpaceKey.Render(_sPress, Color.LightSteelBlue), _rctPress.Location);
		}
		
		public void Init()
		{
			// Initializing the logo surface
			_sfLogo = SurfaceManager.GetSurface("gfx/logo.png");
			// Resizing the surface
			float ratio = Math.Min((70.0f*ScreenManager.Height) / (100.0f*_sfLogo.Height),
			                 (70.0f*ScreenManager.Width) / (100.0f*_sfLogo.Width));
			_sfLogo = _sfLogo.CreateStretchedSurface(new Size(Convert.ToInt32(_sfLogo.Width*ratio), 
			                               Convert.ToInt32(_sfLogo.Height*ratio)));
			_rctLogo = new Rectangle(Convert.ToInt32((ScreenManager.Width-_sfLogo.Width)/2),
			                           Convert.ToInt32((ScreenManager.Height-_sfLogo.Height)/2),
			                           _sfLogo.Width, _sfLogo.Height);
		}
		
		#endregion
		
		#region Misc Functions
		private void ActivePressDisplay()
		{
			_bDisplayPress = true;
		}
		
		private void InitTexts()
		{		
			// Title initialization
			_sTitle = LanguageManager.Get("TS_Title");
			_fTitle = new Sdl.Font(_sFont, 40);
			_rctTitle = new Rectangle(new Point(40,40),
			                            _fTitle.SizeText(_sTitle));
			// SubTitle initialization
			_sSubtitle = LanguageManager.Get("TS_Subtitle");
			_fSubtitle = new Sdl.Font(_sFont, 32);
			_rctSubtitle = new Rectangle(new Point(60,100),
			                               _fSubtitle.SizeText(_sSubtitle));
			
			// Web Information initialization
			_sWeb = LanguageManager.Get("TS_Web");
			_fWeb = new Sdl.Font(_sFont, 18);
			_rctWeb = new Rectangle(new Point(0,5),
			                          _fWeb.SizeText(_sWeb));
			_rctWeb.X = ScreenManager.Width - _rctWeb.Width - 5;
			
			// Version initialization
			_sVersion = LanguageManager.Get("TS_Version") + 
					FileVersionInfo.GetVersionInfo("blindshark.exe").FileVersion;
			_fVersion = new Sdl.Font(_sFont, 12);
			_rctVersion = new Rectangle(new Point(5,0),
			                          _fWeb.SizeText(_sVersion));
			_rctVersion.Y = ScreenManager.Height - _rctVersion.Height;


			// "Press Escape" initialization
			_sPress = LanguageManager.Get("TS_Press");
			_fPressSpaceKey = new Sdl.Font(_sFont, 32);
			_rctPress = new Rectangle(new Point(5,0),
			                            _fPressSpaceKey.SizeText(_sPress));
			_rctPress.Y = ScreenManager.Height - _rctPress.Height - 10;
			_rctPress.X = (ScreenManager.Width - _rctVersion.Width - _rctPress.Width - 5)/2 
				+ _rctVersion.Width+5;
			
			
		}
		#endregion
	}
}
